<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\Permissions;
use App\Http\Models\RoleHasPermissions;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

/**
 * 系统设置控制器
 */
class SystemSetController extends Controller
{
    use AuthenticatesUsers;

    //用户信息
    protected $userInfo;

    public function __construct()
    {
        $this->userInfo = auth()->user();
    }

    /**
     * 获取系统菜单函数
     * 用户获取全部菜单 ：一级，二级，三级等
     * 不包含页面按钮
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenu()
    {
        //获取用户信息
        $userInfo = $this->userInfo;
        //获取角色
        $role_id = RoleHasPermissions::getRolesByUserId($userInfo['id'], ['role_id']);
        $roles = array_column($role_id, 'role_id');
        //获取权限
        $permission = RoleHasPermissions::getPermissionsByRoles($roles);
        $permissions = array_unique(array_column($permission, 'permission_id'));

        $where = [['menu_type', '=', '1']];
        $fields = ['id', 'pid', 'name', 'url', 'title', 'target', 'sort', 'menu_type', 'level'];
        //获取权限菜单
        $rt = RoleHasPermissions::getPermissionsByIds($permissions, $where, $fields);
        $rt = objectToArray($rt);
        $rs = generateTree($rt);

        return respondWithData(0, $rs, '请求成功');
    }


    /**
     * 获取用户所有按钮权限接口
     * @return \Illuminate\Http\JsonResponse
     */
    public function getButtonPermissions()
    {
        //获取用户信息
        $userInfo = $this->userInfo;
        //获取角色
        $role_id = RoleHasPermissions::getRolesByUserId($userInfo['id'], ['role_id']);
        $roles = array_column($role_id, 'role_id');
        //获取权限
        $permission = RoleHasPermissions::getPermissionsByRoles($roles);
        $permissions = array_unique(array_column($permission, 'permission_id'));
        $where = [['menu_type', '=', '0']];
        $fields = ['id'];
        //获取所有按钮权限ID
        $rt = RoleHasPermissions::getPermissionsByIds($permissions, $where, $fields);
        $rs = array_column($rt, 'id');
        return respondWithData(0, $rs, '请求成功');
    }

    /**
     * 系统设置-菜单设置-菜单设置
     * 列表数据 和 下拉树形菜单数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenuList()
    {
        //获取所有按钮权限ID
        $request = request(['menu_type']);//menu_type=1 全部 菜单和按钮  menu_type=2 只有菜单
        $where = $request['menu_type'] == 1 ? [0, 1] : [1];

        $fields = ['id', 'pid', 'name', 'url', 'title', 'target', 'sort', 'menu_type', 'level'];
        $rs = RoleHasPermissions::getMenuList($where, $fields);
        $rt = objectToArray($rs);
        //排序的原因 先弄成树形 再打平
        $rets = generateTree($rt);
        $data = treeToList($rets);

        return respondWithData(0, $data, '请求成功');
    }

    /**
     * 添加菜单
     * @return \Illuminate\Http\JsonResponse
     */
    public function add()
    {
        $request = request([ 'pid', 'name', 'title', 'url', 'sort', 'status', 'level', 'icon', 'menu_type']);

        $model = new \App\Http\Models\Permissions();

        $model->pid = trim($request['pid']);
        $model->name = $request['name'];
        $model->title = $request['title'];
        $model->url = $request['url'];
        $model->sort = (int)$request['sort'];
        $model->level = (int)$request['level'];//层级
        $model->icon = $request['icon'];
        $model->menu_type = (int)$request['menu_type'];//0:按钮 1：菜单

        if ($model->pid >= 0 && is_numeric($model->pid)) {

        } else {
            return respondWithData(0, [], '请求参数错误:pid不能小于0 ');
        }

        if (empty($model->name) || empty($model->title) || empty($model->url)) {
            return respondWithData(0, [], '请求参数错误:name,title,url 不能为空,pid不能小于0 ');
        }
        //判断是否重复
        $where = [
            //'id' => $model->id,
            'name' => $model->name,
            'title' => $model->title,
            'url' => $model->url,
        ];
        //
        $rt = $model::getListByWhere($where,0);
        if (!empty($rt)) {
            return respondWithData(0, [], '请求参数错误:url,title,name,不能重复 ');
        }

        $model->save();
        return respondWithData(0, [], '请求成功');

    }

    /**
     * 菜单编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit()
    {
        $request = request(['id', 'pid', 'name', 'title', 'url', 'sort', 'status', 'level', 'icon', 'menu_type']);

        $M = new \App\Http\Models\Permissions();
        $model = $M::find($request['id']);

        $model->pid = trim($request['pid']);
        $model->name = $request['name'];
        $model->title = $request['title'];
        $model->url = $request['url'];
        $model->sort = (int)$request['sort'];
        $model->level = (int)$request['level'];//层级
        $model->icon = $request['icon'];
        $model->menu_type = (int)$request['menu_type'];//0:按钮 1：菜单

        if ($model->pid >= 0 && is_numeric($model->pid)) {

        } else {
            return respondWithData(0, [], '请求参数错误:pid不能小于0 ');
        }

        if (empty($model->name) || empty($model->title) || empty($model->url)) {
            return respondWithData(0, [], '请求参数错误:name,title,url 不能为空,pid不能小于0 ');
        }
        //判断是否重复
        $where = [
            'id' => $model->id,
            'name' => $model->name,
            'title' => $model->title,
            'url' => $model->url,
        ];
        $rt = $M::getListByWhere($where);
        if (!empty($rt)) {
            return respondWithData(0, [], '请求参数错误:url,title,name,不能重复 ');
        }

        $model->save();
        return respondWithData(0, [], '请求成功');

    }

    /**
     * 菜单排序
     * @return void
     */
    public function setSort()
    {
        $request = request(['id',  'sort']);
        $M = new \App\Http\Models\Permissions();
        $model = $M::find($request['id']);
        $model->sort = (int)$request['sort'];
        $model->save();
        return respondWithData(0, [], '请求成功');
    }

    /**
     * 删除菜单
     * @return void
     */
    public function delMenu()
    {
        $request = request(['id']);
        $ids=explode(',',$request['id']);
        $M = new \App\Http\Models\Permissions();
        $rt=$M->delByWhereIn($ids);
        if($rt){
            return respondWithData(0, [], '请求成功');
        }else{
            return respondWithData(0, [], '请求失败');
        }
    }

}
